<template>
  <div>
    <el-col :span="24" style="height: 100%;">
      <div style="height: 100%;">
        <el-row style="height: 150px;padding-top: 50px" type=“flex” align=“middle”>
          <SearchBox></SearchBox>
        </el-row>
        <el-row>
          <el-table
            :max-height="700"
            v-loading="loading" :data="reservationList"
            @selection-change="handleSelectionChange"
          >
            <el-table-column label="编号" prop="id"
                             :show-overflow-tooltip="true" width="120"
            />
            <el-table-column label="学生" prop="studentName"
                             :show-overflow-tooltip="true" width="150"
            />
            <el-table-column label="状态" :show-overflow-tooltip="true"
                             width="150">
              <template slot-scope="scope">
                {{statusList[scope.row.status].label}}
              </template>
            </el-table-column >
            <el-table-column label="问题描述" prop="description"
                             :show-overflow-tooltip="true" width="500"
            />
            <el-table-column label="操作" align="center"
                             class-name="small-padding fixed-width"
            >
              <div slot-scope="scope" >
                <el-button size="mini" icon="el-icon-edit"
                           @click="handleUpdate(scope.row)"
                >给出建议</el-button
                >
                <el-button size="mini" icon="el-icon-check"
                           @click="finish(scope.row)"
                >完成咨询</el-button
                >
              </div>
            </el-table-column>
          </el-table>
          <pagination v-show="total > 0"
                      :total="total" :page.sync="queryParams.pageNum"
                      :limit.sync="queryParams.pageSize" @pagination="getPage"
          />

          <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">

              <el-form-item label="问题描述" prop="description">
                <el-input v-model="form.description" disabled="true" type="textarea" :autosize="{minRows:3,maxRows:20}"/>
              </el-form-item>

              <el-form-item label="建议" prop="advice">
                <el-input v-model="form.advice" type="textarea" :autosize="{minRows:10,maxRows:20}"/>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="submitForm">确 定</el-button>
              <el-button @click="cancel">取 消</el-button>
            </div>
          </el-dialog>
        </el-row>
      </div>
    </el-col>
  </div>
</template>
<script>
import Vertical from "@/layout/ManageMenu";
import Pagination from "@/components/Pagination/index";
import SearchBox from "@/views/reservation/search";
import {advice, finish, getReservationPage} from "@/api/reservation";
import {addConsult, updateConsult} from "@/api/consult";

export default {
  name: 'ReservationView',
  components: {
    SearchBox,
    Vertical,
    Pagination
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      scoreOpen: false,
      scores: {},

      // 日期范围
      dateRange: [],
      // 菜单列表
      menuOptions: [],
      // 部门列表
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        consultId: 0,
      },
      // 表单参数
      form: {
        advice:"",
        description:""
      },
      // 表单校验
      rules: {

      },
      defaultProps: {
        children: "children",
        label: "label",
      },

      statusList:[
        {
          id:0,
          label:"待完成"
        },
        {
          id:1,
          label:"已完成"
        },
      ],
    };
  },
  created() {
    this.getPage(this.queryParams);
  },
  methods: {
    submitForm: function () {
      advice(this.form)
        .then((response) => {
          if (response.success) {
            this.$alert("修改成功", {
              confirmButtonText: "确认",
              callback: (action) => {
                // console.log('请检查后重新输入')
                this.$router.go(0)//刷新页面
              },
            });
            this.open = false;
            // this.queryParams.pageNum=1
            // this.getPage(this.queryParams)
          } else {
            throw new Error("出现错误", response.message);
          }
        })
    },
    //分页查询
    getPage(data) {
      this.queryParams.consultId=this.$route.query.consultId
      this.loading = true;
      getReservationPage(data)
        .then((response) => {
          if (response.success) {
            this.reservationList = response.data.records;
            if(this.total===0){
              this.total = response.data.total;//重要，会决定有几个页码按钮
            }
            this.loading = false;
          }
        })
        .catch((e) => {
          _this.$message.error(e, "出现未知错误");
        });
    },

    // 新增按钮
    handleAdd(){
      this.reset();
      this.open=true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      // this.reset();
    },
    // 修改按钮
    handleUpdate(row) {
      this.open=true;
      // this.reset();
      this.reservationList.forEach((item) => {
        if (item.id === row.id) {
          this.form.id = item.id
          this.form.description=item.description
          this.form.advice=item.advice
          this.open = true;
          this.title = "咨询建议";
        }
      });
    },
    finish(row){
      alert(JSON.stringify(row))
      finish(row)
        .then((response) => {
          if (response.success) {
            this.$alert("操作成功", {
              confirmButtonText: "确认",
              callback: (action) => {
                // console.log('请检查后重新输入')
                this.$router.go(0)
              },
            });
          }
        })
        .catch((e) => {
          _this.$message.error(e, "出现未知错误");
        });
    },
    // 表单重置
    reset() {
      this.form = {
        id:0,
        advice:"",
      };
    },

  },
}
</script>

<style lang="scss" scoped>
.auto-generate-form {
  padding-top: 50px;
  padding-right: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
</style>
